package org.example.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.aliyun.oss.ServiceException;
import org.example.domain.PoArriveorderVo;
import org.example.service.EasyExcelService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class EasyExcelServiceImpl implements EasyExcelService {

    private static final String TEMPLATE_FILE_PATH = "template/excel/orderTemplate.xlsx";
    private static final String CHARSET_UTF8 = "utf-8";

    @Override
    public void excelTemplate() {
        InputStream templateFileName = null;
        try {
            templateFileName = new ClassPathResource(TEMPLATE_FILE_PATH).getInputStream();
        } catch (IOException e) {
            throw new ServiceException("读取模板异常" + e.getMessage());
        }
        ServletOutputStream out = null;
        try {
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setCharacterEncoding(CHARSET_UTF8);
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("测试模板导出", "UTF-8") + ".xlsx");
            out = response.getOutputStream();
            try (ExcelWriter excelWriter = EasyExcel.write(out).withTemplate(templateFileName).build()) {
                // 初始化写入的Sheet配置
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                // 创建一个Map对象，用于存储待填充到Excel中的数据
                Map<String, Object> map = new HashMap<>();
                // 构建填充配置，设置是否强制换行
                FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
                //填充单个数据
                map.put("title", "上海xxxx有限公司结算单");
                map.put("describe", "上海xxxx有限公司结算单");
                // 填充列表数据，设置“是”强置换行，因为列表下面有备注，所以需要强制换行，不然会覆盖
                excelWriter.fill(dateApply(), fillConfig, writeSheet);
                // 填充Map中的数据
                excelWriter.fill(map, writeSheet);
            }

        } catch (IOException e) {
            throw new ServiceException("导出异常：" + e.getMessage());
        } finally {
            if (templateFileName != null) {
                try {
                    templateFileName.close();
                } catch (IOException e) {
                    throw new ServiceException("关闭流异常：" + e.getMessage());
                }
            }
        }
    }
    private List<PoArriveorderVo> dateApply() {
        List<PoArriveorderVo> poArriveorderVoList = new ArrayList<>();
        // 创建第一个 PoArriveorderVo 对象
        PoArriveorderVo vo1 = new PoArriveorderVo();
        vo1.setIndex("1");
        vo1.setId("ZSYS15052025040300001");
        vo1.setWeighingNo(" GB3WN4250331151437MR");
        vo1.setSupplierName("呼伦⻉尔市华亿物资销售有限公司");
        vo1.setPackageBuckleHeavy(new BigDecimal("500.00"));
        vo1.setSrcOrderNo("NC001");
        vo1.setMaterialCode("M001");
        vo1.setMaterialName("钢材");
        vo1.setMaterialInfo("xxxx aaaa");
        vo1.setInventNum(new BigDecimal("100"));
        vo1.setLength(new BigDecimal("2.0"));
        vo1.setWidth(new BigDecimal("1.0"));
        vo1.setHeight(new BigDecimal("0.5"));
        vo1.setConversionStandard(new BigDecimal("1.0"));
        vo1.setArriveNum(new BigDecimal("200.00"));
        vo1.setTheoreticalAcceptanceWeight(new BigDecimal("480.00"));
        vo1.setOrgName("收货公司A");
        vo1.setPkOrgV("C001");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        vo1.setUpdateDate(null);
        vo1.setVbillcode("V001");
        vo1.setDmakedate(null);
        poArriveorderVoList.add(vo1);

        // 创建第二个 PoArriveorderVo 对象
        PoArriveorderVo vo2 = new PoArriveorderVo();
        vo2.setIndex("2");
        vo2.setId("ZSYS15052025040300001");
        vo2.setWeighingNo("ZSYS150520250403009955");
        vo2.setSupplierName("呼伦⻉尔市华亿物资销售有限公司");
        vo2.setPackageBuckleHeavy(new BigDecimal("600.00"));
        vo2.setSrcOrderNo("NC002");
        vo2.setMaterialCode("M002");
        vo2.setMaterialName("木材");
        vo2.setInventNum(new BigDecimal("200"));
        vo2.setLength(new BigDecimal("3.0"));
        vo2.setWidth(new BigDecimal("1.5"));
        vo2.setHeight(new BigDecimal("0.8"));
        vo2.setConversionStandard(new BigDecimal("1.2"));
        vo2.setArriveNum(new BigDecimal("300.00"));
        vo2.setTheoreticalAcceptanceWeight(new BigDecimal("598.24"));
        vo2.setOrgName("收货公司B");
        vo2.setPkOrgV("C002");
        vo2.setUpdateDate(new Date());
        vo2.setVbillcode("V002");
        vo2.setDmakedate(new Date());
        poArriveorderVoList.add(vo2);
        return poArriveorderVoList;
    }
}
